﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public static class DBHelper
    {
        private static readonly string conntr = ConfigurationManager.ConnectionStrings["QQGy"].ConnectionString;
        public static object ExecuteScalar(string sql, params SqlParameter[] parameter)
        {
            using (SqlConnection con = new SqlConnection(conntr))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    if (parameter != null)
                    {
                        com.Parameters.AddRange(parameter);
                    }
                    object i = com.ExecuteScalar();
                    return i;
                }
            }
        }
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameter)
        {
            SqlConnection con = new SqlConnection(conntr);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            using (SqlCommand com = new SqlCommand(sql, con))
            {
                if (parameter != null)
                {
                    com.Parameters.AddRange(parameter);
                }
                return com.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameter)
        {
            SqlConnection con = new SqlConnection(conntr);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            using (SqlCommand com = new SqlCommand(sql, con))
            {
                if (parameter != null)
                {
                    com.Parameters.AddRange(parameter);
                }
                return com.ExecuteNonQuery();
            }
        }
    }
}